import random		
from formencode import Schema, validators

class Tag(object):

	def __init__(self, *args):
	
		if len(args) == 1:
			self.title, = args 
			self.id = hex(hash(random.random()))[2:]
		elif len(args) == 3: 
			self.id,self.title,self.description = args

	def __repr__(self):
		return '<%s %r>' % (type(self).__name__, self.id)
		
class TagForm(Schema):
	title = validators.UnicodeString(not_empty=True)
	description = validators.UnicodeString()
	

class Poll(object):

	def __init__(self, *args):
		assert 4 < len(args) < 7
		if len(args) == 5:
			self.title,self.minutes,self.sensor,self.arguments,self.state = args
			self.id = hex(hash(random.random()))[2:]
		else:
			self.id,self.title,self.minutes,self.sensor,self.arguments,self.state = args

		
	def __repr__(self):
		return '<%s by %r>' % (type(self).__name__, self.title)	 
			
class PollForm(Schema):
	title = validators.UnicodeString(not_empty=True)
	sensor = validators.UnicodeString(not_empty=True)
	arguments = validators.UnicodeString(not_empty=True)
	minutes = validators.Number(not_empty=True)	


class Event(object):

	def __init__(self, id, tag_id, poll_id,time,status):
		self.id = id
		self.tag_id = tag_id
		self.poll_id = poll_id
		self.time = time
		self.status = status
		
	def __repr__(self):
		return '<%s %r>' % (type(self).__name__, self.id)


class Trigger(object):

	def __init__(self, *args):
		assert 4 < len(args) < 7
		if len(args) == 5:
			self.id = hex(hash(random.random()))[2:]
			self.tag_id,self.description,self.action,self.arguments,self.timing = args
		else:
			self.id,self.tag_id,self.description,self.action,self.arguments,self.timing = args
		
		
	def __repr__(self):
		return '<%s %r>' % (type(self).__name__, self.id)
		
class TriggerForm(Schema):
	description = validators.UnicodeString(not_empty=True)
	action = validators.UnicodeString(not_empty=True)
	arguments = validators.UnicodeString(not_empty=True)
	timing = validators.Number(not_empty=True)
	tag_id = validators.UnicodeString(not_empty=True)
	trigger_id = validators.UnicodeString()